import {
    updateemployeeavatar,
    updatemobilebyuserId,
    uploademployeeavatar,
    getbusinesscard,
    // getUserInfo,
    getEmployeeinfo,
    getILeader,
    getInformation,
    updateUserStatus,
    accountForget,
    getEmployeeexport,
    updateUserInfo
} from '@/services/user';
import { getWorkingDays } from '@/services/notice/homePage';
import pageTabUtil from '@/utils/pageTabUtil';

export default {
    namespace: 'userInfo',

    state: {
        userData: {
            address: '',
            detailAddress: '',
            areaCode: '',
            belongToTheTube: null,
            birthday: '',
            certificate: '',
            departmentName: '',
            email: '',
            postType: '',
            employeeName: '',
            employeeRankName: '',
            graduateSchool: '',
            highestDegreeName: '',
            immediateSuperiorCode: null,
            immediateSuperior: '',
            inServiceStatusName: '',
            jobDescriptions: '',
            jobLabel: [],
            major: '',
            mobile: '',
            nationalArea: '',
            departmentPath: '',
            postName: '',
            sexTypeName: '',
            status: null,
            teacher: '',
            timeOfEntry: '',
            userCode: null,
            userName: '',
            userNo: null,
            roleNames: [],
            paramAfterEncrypt: '',
            vvCourseVos: [
                {
                    changeType: '',
                    createTime: '',
                    department: '',
                    id: null,
                    instructions: '',
                    post: '',
                    postDescription: '',
                    typeAndRank: '',
                    vvDatetime: ''
                }
            ],
            isEnabled: undefined,
            resetModalSuccess: null
        }
    },

    effects: {
        /**
         * 上传个人头像
         */
        *uploademployeeavatar({ payload, callback }, { call }) {
            const response = yield call(uploademployeeavatar, payload);
            callback(response);
        },
        /**
         * 更新个人头像
         */
        *updateemployeeavatar({ payload, callback }, { call }) {
            const response = yield call(updateemployeeavatar, payload);
            callback(response);
        },
        /**
         * 修改手机号码
         */
        *updatemobilebyuserId({ payload, callback }, { call }) {
            const response = yield call(updatemobilebyuserId, payload);
            callback(response);
        },
        // 获取个人信息
        *getUserInfo({ payload, callback }, { call, put }) {
            const response = yield call(getEmployeeinfo, payload);
            if (response.code === 10000) {
                yield put({
                    type: 'save',
                    payload: {
                        userData: response.data
                    }
                });
            }
            callback(response);
        },
        // 获取上级信息
        *getILeader({ payload, callback }, { call, put }) {
            const response = yield call(getILeader, payload);
            if (response.code === 10000) {
                yield put({
                    type: 'save',
                    payload: {
                        userData: response.data
                    }
                });
            }
            callback(response);
        },
        *getbusinesscard({ payload }, { call, put }) {
            const response = yield call(getbusinesscard, payload);
            yield put({
                type: 'save',
                payload: {
                    userData: response.data
                }
            });
        },
        *fetchInformation({ payload, callback }, { call }) {
            const response = yield call(getInformation, payload);
            callback(response);
        },
        // 解禁|禁用员工
        *fetchDisableuser({ payload, callback }, { call }) {
            const response = yield call(updateUserStatus, payload);
            callback(response);
        },
        // 重置密码
        *fetchAccountForget({ payload, callback }, { call }) {
            const response = yield call(accountForget, payload);
            callback(response);
        },
        // 导出员工个人档案所有信息
        *fetchEmployeeexport({ payload, callback }, { call }) {
            const response = yield call(getEmployeeexport, payload);
            callback(response);
        },
        // 获取天数
        *fetchWorkingDays({ payload, callback }, { call }) {
            const response = yield call(getWorkingDays, payload);
            callback(response);
        },
        // 保存
        *saveUserInfo({ payload, callback }, { call }) {
            const response = yield call(updateUserInfo, payload);
            callback(response);
        }
    },
    reducers: {
        save(state, { payload }) {
            return {
                ...state,
                ...payload
            };
        },
        clear(state) {
            return {
                ...state,
                userData: {
                    address: '',
                    detailAddress: '',
                    areaCode: '',
                    belongToTheTube: null,
                    birthday: '',
                    certificate: '',
                    departmentName: '',
                    email: '',
                    postType: '',
                    employeeName: '',
                    employeeRankName: '',
                    graduateSchool: '',
                    highestDegreeName: '',
                    immediateSuperiorCode: null,
                    immediateSuperior: '',
                    inServiceStatusName: '',
                    jobDescriptions: '',
                    jobLabel: [],
                    major: '',
                    mobile: '',
                    nationalArea: '',
                    departmentPath: '',
                    postName: '',
                    sexTypeName: '',
                    status: null,
                    teacher: '',
                    timeOfEntry: '',
                    userCode: null,
                    userName: '',
                    userNo: null,
                    roleNames: [],
                    paramAfterEncrypt: '',
                    vvCourseVos: [
                        {
                            changeType: '',
                            createTime: '',
                            department: '',
                            id: null,
                            instructions: '',
                            post: '',
                            postDescription: '',
                            typeAndRank: '',
                            vvDatetime: ''
                        }
                    ]
                }
            };
        }
    },
    subscriptions: {
        setup({ dispatch, history }) {
            // Subscribe history(url) change, trigger `load` action if pathname is `/`
            let oldPathname = '';
            return history.listen(({ pathname }) => {
                // 有使用到 userInfo 数据, 且需要特殊处理的页面
                // 第一个条件: 我的档案->查看直接上级的场景
                // 第二个条件: hash 变化的情况下(滚动条定位) 不清除数据; 查看个人档案详情->滚动条定位导致的url变化的场景
                // 原作者的逻辑很绕, userInfo.employeeCode 会被用于请求其他数据, 如果不清楚掉, 数据会错乱
                if (
                    (!oldPathname || oldPathname.indexOf('/personal/myInfo') > -1) && // 确保是当前页面之间的跳转(参数变动而已)
                    pathname.indexOf('/personal/myInfo') > -1 && // 主要是 <UserInfo> 这个组件有问题, 在 personal/myInfo 这个页面因为"查看直接上级"功能有问题
                    pathname.indexOf('/myInfoDetail') === -1 // 排除掉其他页面
                ) {
                    // 清除当前的数据
                    dispatch({
                        type: 'clear'
                    });
                    // 触发 refreshTab 刷新数据
                    // 当前页面的刷新. 场景: 查看直接上级;
                    if (pageTabUtil.history[pathname] && oldPathname && oldPathname !== pathname) {
                        pageTabUtil.refreshTab(pathname);
                    }
                }
                oldPathname = pathname; // 记录上个页面, 避免在回退过程/切换选项卡过程中导致的问题
            });
        }
    }
};
